package com.remote.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.base.Joiner;
import com.remote.core.RemoteCommandService;
import com.remote.entity.FileItem;
import com.remote.framework.CommandShell;
import com.remote.framework.RestResultDto;

@Controller
public class RemoteController {

	@Autowired
	private RemoteCommandService remoteCommandService;
	@Autowired
	private HttpSession httpSession;
	private static final String ROOT = "/";

	@RequestMapping("/excuteStart")
	@ResponseBody
	public RestResultDto excuteStart() {
		return remoteCommandService.execute(CommandShell.CMD_START_WXWALK);
	}

	@RequestMapping("/excuteStop")
	@ResponseBody
	public RestResultDto excuteStop() {
		return remoteCommandService.execute(CommandShell.CMD_STOP_WXWALK);
	}

	@RequestMapping("/list")
	public ModelAndView list(String dirPath, boolean isVertul, Model model) {
		List<FileItem> list = null;
		Object connInfoSession = httpSession.getAttribute("connInfo");
		Map<String,String> connInfo= (Map<String, String>)connInfoSession ;
		if (StringUtils.isEmpty(dirPath)) {
			model.addAttribute("dirPath", ROOT);
			list = remoteCommandService.getFiles(connInfo,ROOT);
		}else {
			if (isVertul) {
				String currentPath = handle(dirPath);
				model.addAttribute("dirPath", currentPath);
				list = remoteCommandService.getFiles(connInfo,currentPath);
			} else {
				model.addAttribute("dirPath", dirPath);
				list = remoteCommandService.getFiles(connInfo,dirPath);
			}
		}
		
		model.addAttribute("allFile", list);
		return new ModelAndView("index");
	}

	private static String handle(String dirPath) {
		List<String> dirList= Arrays.asList(dirPath.split("/"));
		if(dirList.size()>3) {
			List<String> newDirList = dirList.subList(0, dirList.size()-2);
			return Joiner.on("/").skipNulls().join(newDirList);
		}
		return "/";
	}

	@RequestMapping("/view")
	public void view(String path, HttpServletResponse response) {
		@SuppressWarnings("unchecked")
		Map<String,String> connInfo= (Map<String, String>) httpSession.getAttribute("connInfo");
		remoteCommandService.view(connInfo,path, response);
	}
	
	@RequestMapping("/downLoad")
	public void downLoad(String path, HttpServletResponse response) {
		@SuppressWarnings("unchecked")
		Map<String,String> connInfo= (Map<String, String>) httpSession.getAttribute("connInfo");
		remoteCommandService.downLoad(connInfo,path, response);
	}
	
	@GetMapping("/")
	public String login(HttpServletRequest req) {
		Object RequestURL = req.getAttribute("RequestURL");
		req.setAttribute("RequestURL", RequestURL);
		return "login";
	}
	
	@PostMapping("/login")
	public ModelAndView loginHandle(HttpServletRequest req,String ip,String username,String password, Model model) {
		RestResultDto login = remoteCommandService.login(ip,username,password);
		if (!login.isSuccess()) {
			model.addAttribute("isFail", true);
			model.addAttribute("err", login.getMessage());
			return new ModelAndView("login");
		}
		httpSession.setAttribute("connInfo", login.getBody());
		String RequestURL = req.getParameter("RequestURL");
		System.out.println("RequestURL="+RequestURL);
		if(!StringUtils.isEmpty(RequestURL)) {
			return new ModelAndView("redirect:"+RequestURL.toString());
		}
		return new ModelAndView("redirect:/list");
	}
	
	@GetMapping("/logout")
	public String logout() {
		httpSession.setAttribute("connInfo",null);
		httpSession.invalidate();
		return "redirect:/";
	}
}
